package cc.rengu.igas.channel.upac.biz.impl;

import cc.rengu.igas.channel.upac.common.constant.UpacServiceConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.facade.UpacCollectionService;
import cc.rengu.igas.channel.upac.facade.request.*;
import cc.rengu.igas.channel.upac.facade.response.*;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class UpacCollectionServiceImpl implements UpacCollectionService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<CollectionSignWithSmsResponse> collectionSignWithSms(CollectionSignWithSmsRequest collectionSignWithSmsRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionSignWithSmsResponse> result = new Result<>();
        CollectionSignWithSmsResponse collectionSignWithSmsResponse = new CollectionSignWithSmsResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionSignWithSmsRequest,
                    UpacServiceConstant.COLLECTION_SIGN_WITHSMS_SERVICE, CollectionSignWithSmsResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionSignWithSmsResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代收签约短信发送失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                collectionSignWithSmsResponse.setRespDesc(((BizException) e).getCode());
                collectionSignWithSmsResponse.setRespDesc(e.getMessage());
            } else {
                collectionSignWithSmsResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                collectionSignWithSmsResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CollectionSignResponse> collectionSign(CollectionSignRequest collectionSignRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionSignResponse> result = new Result<>();
        CollectionSignResponse collectionSignResponse = new CollectionSignResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionSignRequest,
                    UpacServiceConstant.COLLECTION_SIGN_SERVICE, CollectionSignResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionSignResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代收签约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                collectionSignResponse.setRespDesc(((BizException) e).getCode());
                collectionSignResponse.setRespDesc(e.getMessage());
            } else {
                collectionSignResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                collectionSignResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CollectionSignCancelResponse> collectionSignCancel(CollectionSignCancelRequest collectionSignCancelRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionSignCancelResponse> result = new Result<>();
        CollectionSignCancelResponse collectionSignCancelResponse = new CollectionSignCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionSignCancelRequest,
                    UpacServiceConstant.COLLECTION_SIGN_CANCEL_SERVICE, CollectionSignCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionSignCancelResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代收解约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                collectionSignCancelResponse.setRespDesc(((BizException) e).getCode());
                collectionSignCancelResponse.setRespDesc(e.getMessage());
            } else {
                collectionSignCancelResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                collectionSignCancelResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CollectionTransStateQueryResponse> collectionTransStateQuery(CollectionTransStateQueryRequest collectionTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionTransStateQueryResponse> result = new Result<>();
        CollectionTransStateQueryResponse transStateQueryResponse = new CollectionTransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionTransStateQueryRequest,
                    UpacServiceConstant.COLLECTION_TRANSTATE_QUERY_SERVICE, CollectionTransStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionTransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代收类交易状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                transStateQueryResponse.setRespDesc(((BizException) e).getCode());
                transStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                transStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                transStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CollectionConsumeCancelResponse> collectionConsumeCancel(CollectionConsumeCancelRequest collectionConsumeCancelRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionConsumeCancelResponse> result = new Result<>();
        CollectionConsumeCancelResponse consumeCancelResponse = new CollectionConsumeCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionConsumeCancelRequest,
                    UpacServiceConstant.COLLECTION_CONSUME_CANCEL_SERVICE, CollectionConsumeCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionConsumeCancelResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代收消费撤销失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                consumeCancelResponse.setRespDesc(((BizException) e).getCode());
                consumeCancelResponse.setRespDesc(e.getMessage());
            } else {
                consumeCancelResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                consumeCancelResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CollectionConsumeRefundResponse> collectionConsumeRefund(CollectionConsumeRefundRequest collectionConsumeRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionConsumeRefundResponse> result = new Result<>();
        CollectionConsumeRefundResponse consumeRefundResponse = new CollectionConsumeRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionConsumeRefundRequest,
                    UpacServiceConstant.COLLECTION_CONSUME_REFUND_SERVICE, CollectionConsumeRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionConsumeRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("代收消费退货失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                consumeRefundResponse.setRespDesc(((BizException) e).getCode());
                consumeRefundResponse.setRespDesc(e.getMessage());
            } else {
                consumeRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                consumeRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CollectionResponse> singleCollection(CollectionRequest collectionRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionResponse> result = new Result<>();
        CollectionResponse collectionResponse = new CollectionResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(collectionRequest,
                    UpacServiceConstant.SINGLE_COLLECTION_SERVICE, CollectionResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CollectionResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("单笔代收失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                collectionResponse.setRespDesc(((BizException) e).getCode());
                collectionResponse.setRespDesc(e.getMessage());
            } else {
                collectionResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                collectionResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BatCollectionResponse> batCollection(BatCollectionRequest batCollectionRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BatCollectionResponse> result = new Result<>();
        BatCollectionResponse batCollectionResponse = new BatCollectionResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(batCollectionRequest,
                    UpacServiceConstant.BAT_COLLECTION_SERVICE, BatCollectionResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BatCollectionResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("批量代收请求失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                batCollectionResponse.setRespDesc(((BizException) e).getCode());
                batCollectionResponse.setRespDesc(e.getMessage());
            } else {
                batCollectionResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                batCollectionResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BatCollectionStateQueryResponse> batCollectionStateQuery(BatCollectionStateQueryRequest batCollectionStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BatCollectionStateQueryResponse> result = new Result<>();
        BatCollectionStateQueryResponse batCollectionStateQueryResponse = new BatCollectionStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(batCollectionStateQueryRequest,
                    UpacServiceConstant.BAT_COLLECTION_STATE_QUERY_SERVICE, BatCollectionStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BatCollectionStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("批量代收状态失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                batCollectionStateQueryResponse.setRespDesc(((BizException) e).getCode());
                batCollectionStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                batCollectionStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                batCollectionStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
